//
//  DR_LookFileVC.swift
//  DocReaderRP
//
//  Created by  mac on 2025/11/14.
//

import UIKit
import QuickLook
import SnapKit

class DR_LookFileVC: DR_BaseVC {
    
    private var fileModel: DR_FileModel?
    private var previewController: QLPreviewController?

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        setupConstraints()
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
    
    // MARK: - 配置文件
    func configure(with fileModel: DR_FileModel) {
        self.fileModel = fileModel
        nameLabel.text = fileModel.displayName
        
        // 设置预览控制器
        setupPreviewController()
    }
    
    private func setupPreviewController() {
        guard let fileModel = fileModel else { return }
        
        previewController = QLPreviewController()
        previewController?.dataSource = self
        previewController?.delegate = self
        
        // 添加预览控制器为子视图控制器
        addChild(previewController!)
        view.addSubview(previewController!.view)
        previewController!.didMove(toParent: self)
        
        // 设置预览控制器的约束
        previewController!.view.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.top.equalTo(backButton.snp.bottom)
        }
    }
    
    @objc private func clickBackButton(){
        self.navigationController?.popViewController(animated: true)
    }
    
    private func setupUI(){
        view.addSubview(backButton)
        view.addSubview(nameLabel)
    }
    
    private func setupConstraints(){
        backButton.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide)
            make.width.equalTo(56)
            make.height.equalTo(44)
        }
        nameLabel.snp.makeConstraints { make in
            make.leading.equalTo(backButton.snp.trailing)
            make.trailing.equalToSuperview().offset(-20)
            make.centerY.equalTo(backButton)
        }
    }
    
    private let backButton: UIButton = {
        let v = UIButton(type: .custom)
        v.setImage("common_back".toImage, for: .normal)
        return v
    }()
    
    private let nameLabel: UILabel = {
        let v = UILabel()
        v.textColor = .black
        v.font = UIFont.systemFont(ofSize: 16)
        v.textAlignment = .left
        return v
    }()
}

// MARK: - QLPreviewControllerDataSource
extension DR_LookFileVC: QLPreviewControllerDataSource {
    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return fileModel != nil ? 1 : 0
    }
    
    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        guard let fileModel = fileModel else {
            fatalError("File model is nil")
        }
        return fileModel.fileURL as QLPreviewItem
    }
}

// MARK: - QLPreviewControllerDelegate
extension DR_LookFileVC: QLPreviewControllerDelegate {
    func previewControllerWillDismiss(_ controller: QLPreviewController) {
        // 预览控制器即将关闭时的处理
    }
}
